<?php
/**
 * QFSHOP 
 * ============================================================================
 * 青峰网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

//配送区域

//区域列表
if($_REQUEST['act']=='list'){
	$shipping_id=$_REQUEST['shipping'];
	$sql = "SELECT a.shipping_area_id,a.shipping_area_name,c.region_name FROM ".ly()."shipping_area a,".ly()."area_region b,".ly()."region c where a.shipping_id='{$shipping_id}' and a.shipping_area_id=b.shipping_area_id and b.region_id=c.region_id ";
	$row=$db->getAll($sql);
	$smarty->assign('shipping_area_list',$row);
	$smarty->assign('shipping_id',$shipping_id);
    $smarty->display('shipping_area_list.html');
}
//添加和编辑配送区域
elseif($_REQUEST['act']=='edit'){
	//配送方式id
	$shipping_id=$_REQUEST['shipping_id'];
	if($shipping_id>0){
		//配送方式区域id
		$shipping_area_id=$_REQUEST['shipping_area_id'];
		$sql2 = "SELECT c.region_name FROM ".ly()."shipping_area a,".ly()."area_region b,".ly()."region c where a.shipping_area_id='{$shipping_area_id}' and a.shipping_area_id=b.shipping_area_id and b.region_id=c.region_id ";
		$row2=$db->getAll($sql2);	
		if($shipping_area_id){
			$sql1="select * from ".ly()."shipping_area where shipping_area_id='{$shipping_area_id}' ";
			$row1=$db->getAll($sql1);
			foreach ($row1 as $key => $value) {
				$con=$value['configure'];
			}
			$arr=unserialize($con);
	   	 	$fields=object_array($arr);
	    	/*array_pop($fields);*/
	   	}
	   	$smarty->assign('fields',$fields);
	}else{
		$config = array();
    	$config['0']['name']     = 'item_fee';
    	$config['0']['value']    = $_REQUEST['item_fee'];
    	$config['1']['name']     = 'base_fee';
    	$config['1']['value']    = $_REQUEST['base_fee'];
    	$config['2']['name']     = 'step_fee';
    	$config['2']['value']    = empty($_POST['step_fee']) ? '' : $_POST['step_fee'];
    	$config['3']['name']     = 'step_fee1';
    	$config['3']['value']    = empty($_POST['step_fee1']) ? '' : $_POST['step_fee1'];
		$config['4']['name']     = 'pack_fee';
    	$config['4']['value']    = empty($_POST['pack_fee']) ? '' : $_POST['pack_fee'];
    	$config['5']['name']     = 'fee_compute_mode';
    	$config['5']['value']    = empty($_POST['fee_compute_mode']) ? '' : $_POST['fee_compute_mode'];
    	$smarty->assign('fields',$config);
	}
	//查询地区
	$sql="select * from ".ly()."region where region_type in(0,1)";
	$row=$db->getAll($sql); 
	$smarty->assign('shipping_id',$shipping_id);
	$smarty->assign('aa',$value);
	$smarty->assign('regions',$row2);
	$smarty->assign('shipping_area_edit',$row);
	$smarty->display('shipping_area_edit.html');
}
elseif($_REQUEST['act']=='insert'){
	$city=$_REQUEST['city'];
	$district=$_REQUEST['district'];
	$shipping_id=$_REQUEST['shipping_id'];
	$shipping_area_id=$_REQUEST['shipping_area_id'];
	$shipping_area_name=$_REQUEST['shipping_area_name'];
	$item_fee=$_REQUEST['item_fee'];
	//配送规则
   	$config = array();
    $config['0']['name']     = 'item_fee';
    $config['0']['value']    = $_REQUEST['item_fee'];
    $config['1']['name']     = 'base_fee';
    $config['1']['value']    = $_REQUEST['base_fee'];
    $config['2']['name']     = 'step_fee';
    $config['2']['value']    = empty($_POST['step_fee']) ? '' : $_POST['step_fee'];
    $config['3']['name']     = 'step_fee1';
    $config['3']['value']    = empty($_POST['step_fee1']) ? '' : $_POST['step_fee1'];
	$config['4']['name']     = 'pack_fee';
    $config['4']['value']    = empty($_POST['pack_fee']) ? '' : $_POST['pack_fee'];
    $config['5']['name']     = 'fee_compute_mode';
    $config['5']['value']    = empty($_POST['fee_compute_mode']) ? '' : $_POST['fee_compute_mode'];
	$configure=serialize($config);
	if($shipping_area_id){
		$sql="update ".ly()."shipping_area set configure='{$configure}' where shipping_area_id='{$shipping_area_id}'";
		$row=$db->query($sql);
		if($district){
			$sql1="update ".ly()."area_region set region_id='{$district}' where shipping_area_id='{$shipping_area_id}'";
			$row1=$db->query($sql1);
		}else{
			$sql1="update ".ly()."area_region set region_id='{$city}' where shipping_area_id='{$shipping_area_id}'";
			$row1=$db->query($sql1);	
		}
		if($row==1&$row1==1){
			$href[]=array('text'=>"返回配送区域列表",'href'=>'shipping_area.php?act=list&shipping='.$_POST['shipping_id']);
        	sys_msg("修改成功！",$href);
  		}else{
  			$href[]=array('text'=>"返回编辑",'href'=>'shipping_area.php?act=edit&shipping='.$_POST['shipping_area_id']);
         	sys_msg("修改失败！",$href);
     	
  		}
	}else{
		$sql="insert into ".ly()."shipping_area(shipping_area_name,shipping_id,configure) values('{$shipping_area_name}','{$shipping_id}','{$configure}')";
		$row=$db->query($sql);	
		$shipping_area_id=mysql_insert_id();
		if($district){
			$sql1="insert into ".ly()."area_region(shipping_area_id,region_id) values('{$shipping_area_id}','{$district}')";
			$row1=$db->query($sql1);	
		}else{
			$sql1="insert into ".ly()."area_region(shipping_area_id,region_id) values('{$shipping_area_id}','{$city}')";
			$row1=$db->query($sql1);	
		}
		if($row==1&$row1==1){
			$href[]=array('text'=>"返回配送区域列表",'href'=>'shipping_area.php?act=list&shipping='.$_POST['shipping_id']);
        	sys_msg("添加成功！",$href);
  		}else{
  			$href[]=array('text'=>"返回编辑",'href'=>'shipping_area.php?act=edit&shipping='.$_POST['shipping_id']);
         	sys_msg("添加失败！",$href);
     	
  		}	
	}
}elseif($_REQUEST['act']=='del'){
	$shipping_area_id=$_REQUEST['shipping_area_id'];
	$sql="delete from ".ly()."shipping_area where shipping_area_id='{$shipping_area_id}'";
	$row=$db->query($sql);
	if($row==1){
		$sql1="delete from ".ly()."area_region where shipping_area_id='{$shipping_area_id}'";
		$row1=$db->query($sql1);
		if($row1==1){
			$href[]=array('text'=>"返回配送区域列表",'href'=>'shipping_area.php?act=list&shipping='.$_GET['shipping_id']);
        	sys_msg("删除成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回配送区域列表",'href'=>'shipping_area.php?act=list&shipping='.$_GET['shipping_id']);
         	sys_msg("删除失败！",$href);
     	}
	}
}
elseif($_REQUEST['act']=='diqu'){
	$id=$_POST['id'];
	$sql="select * from ".ly()."region where parent_id='{$id}'";
	$row=$db->getAll($sql);
	$smarty->assign('shipping_area_edit',$row);
	$smarty->display('shipping_area_edit.html');
}
elseif($_REQUEST['act']=='name'){
	$shipping_area_name=$_REQUEST['shipping_area_name'];
	$id=$_REQUEST['id'];
	$sql="select * from ".ly()."shipping_area where shipping_id='{$id}'and shipping_area_name='{$shipping_area_name}'";
	$row=$db->getRow($sql);
	$arr=mysql_affected_rows();
	if($arr > 0){
		echo 1;
	}else{
		echo 2;
	}
}
function object_array($array) {  
    if(is_object($array)) {  
        $array = (array)$array;  
     } if(is_array($array)) {  
         foreach($array as $key=>$value) {  
             $array[$key] = object_array($value);  
             }  
     }  
     return $array;  
}
?>


